package com.yzy.fly.mapper;

import com.yzy.fly.entity.role.FlyModel;
import com.yzy.fly.kernel.mapper.FlyBasicMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.Collection;
import java.util.List;

@Mapper
public interface FlyModelMapper extends FlyBasicMapper<FlyModel> {

    @Select("select * from fly_model a where 1=1 order by seq asc")
    List<FlyModel> selectAllList();

    @Select("select * from fly_model a where 1=1 and a.systemId = #{systemId} order by seq asc")
    List<FlyModel> selectAllBySystemId(@Param("systemId") Integer systemId );

    @Select("select * from fly_model a where 1=1 and a.parentId = #{parentId} and a.systemId = #{systemId} order by seq asc")
    List<FlyModel> selectAllByParentIdAndSystemId(@Param("parentId") Integer parentId, @Param("systemId") Integer systemId);

    @Select("select * from fly_model a where 1=1 and (a.id = #{parentId} or a.parentId = #{parentId}) and a.systemId = #{systemId} order by seq asc")
    List<FlyModel> selectAllByParentIdAndSystemId_ContainsId(@Param("parentId") Integer parentId, @Param("systemId") Integer systemId);

    @Select("select * from fly_model a where 1=1 and a.defModelId = #{defModelId} and a.systemId = #{systemId} order by seq asc")
    List<FlyModel> selectAllByDefIdIdAndSystemId(@Param("defModelId") Integer defModelId, @Param("systemId") Integer systemId);

    @Select("select * from fly_model a where 1=1 and a.parentId = #{parentId} order by seq asc")
    List<FlyModel> selectAllByParentId(@Param("parentId") Integer parentId);

    /**
     * 批量插入（mysql）
     * @param entityList
     * @return
     */
    Integer insertBatchSomeColumn(Collection<FlyModel> entityList);


    @Update("update fly_model a set seq = seq - 1  where id = #{id} and seq = #{seq}")
    int upById(@Param("id") int id, @Param("seq")int seq);

    @Update("update fly_model a set seq = seq + 1  where id = #{id} and seq = #{seq}")
    int downById(@Param("id")int id, @Param("seq")int seq);

    @Update({"<script>",
            "update fly_model a set seq = seq - 1  where 1=1 and ",
            "<choose>",
            "<when test='parentId == null'>",
            "parentId is null ",
            "</when>",
            "<otherwise>",
            "parentId = #{parentId} ",
            "</otherwise>",
            "</choose>",
            "and defModelId = #{defModelId} and seq = #{seq}",
            "</script>"})
    int upByParentIdAndDefModelId(@Param("parentId") Integer parentId, @Param("defModelId") int defModelId, @Param("seq")int seq);

    @Update({"<script>",
            "update fly_model a set seq = seq + 1  where 1=1 and ",
            "<choose>",
            "<when test='parentId == null'>",
            "parentId is null ",
            "</when>",
            "<otherwise>",
            "parentId = #{parentId} ",
            "</otherwise>",
            "</choose>",
            "and defModelId = #{defModelId} and seq = #{seq}",
            "</script>"})
    int downByParentIdAndDefModelId(@Param("parentId") Integer parentId,@Param("defModelId")int defModelId, @Param("seq")int seq);

    @Select({"<script>",
            "select max(a.seq) from fly_model a where 1=1 and ",
            "<choose>",
            "<when test='parentId == null'>",
            "parentId is null ",
            "</when>",
            "<otherwise>",
            "parentId = #{parentId} ",
            "</otherwise>",
            "</choose>",
            "and defModelId = #{defModelId}",
            "</script>"})
    Integer maxSeqNo(@Param("parentId")Integer parentId,@Param("defModelId")int defModelId);

    @Select({"<script>",
            " select *",
            " from fly_model a ",
            " where 1=1",
            "<when test='label!=null and label!=\"\"'>",
            "  and a.label like #{label,jdbcType=VARCHAR}",
            "</when>",
            "<when test='name!=null and name!=\"\"'>",
            "  and a.name like #{name,jdbcType=VARCHAR} ",
            "</when>",
//            "<when test='fieldName!=null and fieldName!=\"\"'>",
//            "  and a.fieldName like #{fieldName,jdbcType=VARCHAR}",
//            "</when>",
            " and a.systemId = #{systemId} order by seq asc",
            "</script>"})
    List<FlyModel> depository(@Param("label")String label, @Param("name") String name, @Param("fieldName")String fieldName,@Param("systemId") Integer systemId );

}
